﻿using LSQL;
using System;
using System.Data;
using System.Windows.Forms;

namespace Shopfloormanagement
{
    public partial class Form_DgvInput : Form
    {
        public int Row;
        public int RowCount;
        public int Day = 0;
        public int FactoryID;
        public int DepartmentID;
        public int Level;
        public string ControlType;
        public int ControlOrder;
        public string KPItype;
        public int Year;
        public int Month;
        public DataGridView dgv;
        public int tp = 0;//存储0和1，因为折线图配红绿和折线图配文字表格都回使用这个窗口输入，但数据更新后的刷新需要区分是红绿还是文字
        public Form_DgvInput()
        {
            InitializeComponent();
        }
        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void btn_Confirm_Click(object sender, EventArgs e)
        {
            string Condition1 = "FactoryID=" + FactoryID + "and DepartmentID=" + DepartmentID + "and Level=" + Level + "and ControlType='" + ControlType + "'and ControlOrder=" + ControlOrder + "and KPItype='" + KPItype + "'and RowType=1 and Year=" + Year + " and Month=" + Month;
            if (SQL.Update(SystemInfo.SQL, "Data_DataGridView", "[" + Day + "]=" + txt_Plan.Text, Condition1) == true)
            {
                if (RowCount == 2)
                {
                    string Condition2 = "FactoryID=" + FactoryID + "and DepartmentID=" + DepartmentID + "and Level=" + Level + "and ControlType='" + ControlType + "'and ControlOrder=" + ControlOrder + "and KPItype='" + KPItype + "'and RowType=2 and Year =" + Year + " and Month=" + Month;
                    SQL.Update(SystemInfo.SQL, "Data_DataGridView", "[" + Day + "]=" + txt_Actual.Text, Condition2);
                }
                Sunny.UI.UIMessageTip.Show("成功！", Sunny.UI.TipStyle.Green);
                if (tp == 1)
                {
                    //等于1时按红绿刷新
                    Form_Department.department.FrameType = "折线图";
                    Form_Department.department.LoadData_DGV1(ControlType, ControlOrder, KPItype, dgv, RowCount);
                }
                else
                {
                    //否则按文字表格刷新
                    Form_Department.department.LoadData_DGV2(ControlType, ControlOrder, KPItype, dgv, RowCount);
                }
                dgv.CurrentCell = dgv.Rows[0].Cells[0];
                this.Close();
                //增加SQDC颜色表数据
                string Condition3 = "FactoryID=" + FactoryID + "and DepartmentID=" + DepartmentID + " and LevelID=" + Level + " and Datetime='" + DateTime.Now.Date + "' and Del_tag=0";
                DataTable dt1 = SQL.ReturnDataTable2(SystemInfo.SQL, "Color_of_SQDC", Condition3);
                if (dt1.Rows.Count > 0)
                {
                    int ID = Convert.ToInt32(dt1.Rows[0]["ID"].ToString());
                }
                else
                {
                    SQL.Insert(SystemInfo.SQL, "Color_of_SQDC", "FactoryID,DepartmentID,LevelID,Datetime,Del_tag", $"{FactoryID},{DepartmentID},{Level},'{DateTime.Now.Date}',{0}");
                }
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string RedGreen = "";
            string Condition3 = "FactoryID=" + FactoryID + "and DepartmentID=" + DepartmentID + "and Level=" + Level + "and ControlType='" + ControlType + "'and ControlOrder=" + ControlOrder + "and KPItype='" + KPItype + "'";
            //获取该条件的公差
            DataTable dt1 = SQL.ReturnDataTable2(SystemInfo.SQL, "Interface", Condition3);

            double Max = Convert.ToDouble(dt1.Rows[0]["MaxTarget"].ToString());
            double Min = Convert.ToDouble(dt1.Rows[0]["MinTarget"].ToString());
            if (RowCount == 1)
            {
                //单行文字
                if (Min != 0 && Max == 0)
                {
                    //只有最小值时
                    if (Convert.ToDouble(txt_Plan.Text) < Min)
                    {
                        RedGreen = "Red";
                    }
                    else
                    {
                        RedGreen = "Green";
                    }
                }
                else if (Min == 0 && Max != 0)
                {
                    if (Convert.ToDouble(txt_Plan.Text) > Max)
                    {
                        RedGreen = "Red";
                    }
                    else
                    {
                        RedGreen = "Green";
                    }
                }
                else if (Min != 0 && Max != 0)
                {
                    if (Convert.ToDouble(txt_Plan.Text) < Min || Convert.ToDouble(txt_Plan.Text) > Max)
                    {
                        RedGreen = "Red";
                    }
                    else
                    {
                        RedGreen = "Green";
                    }
                }

            }
            else
            {
                //双行文字
                double Val = Convert.ToDouble(txt_Actual.Text) * 100 / Convert.ToDouble(txt_Plan.Text);
                Console.WriteLine(Val);
                if (Min != 0 && Max == 0)
                {
                    //只有最小值时
                    if (Val < Min)
                    {
                        RedGreen = "Red";
                    }
                    else
                    {
                        RedGreen = "Green";
                    }
                }
                else if (Min == 0 && Max != 0)
                {
                    if (Val > Max)
                    {
                        RedGreen = "Red";
                    }
                    else
                    {
                        RedGreen = "Green";
                    }
                }
                else if (Min != 0 && Max != 0)
                {
                    if (Val < Min || Val > Max)
                    {
                        RedGreen = "Red";
                    }
                    else
                    {
                        RedGreen = "Green";
                    }
                }
            }
            Console.WriteLine(RedGreen);
            ////增加SQDC颜色表数据
            //string Condition4 = "FactoryID=" + FactoryID + "and DepartmentID=" + DepartmentID + " and LevelID=" + Level + " and Datetime='" + DateTime.Now.Date + "' and Del_tag=0";
            //DataTable dt2 = SQL.ReturnDataTable2(SystemInfo.add, "Color_of_SQDC", Condition4);
            //if (dt2.Rows.Count > 0)
            //{
            //    int ID = Convert.ToInt32(dt2.Rows[0]["ID"].ToString());
            //}
            //else
            //{
            //    SQL.Insert(SystemInfo.add, "Color_of_SQDC", "FactoryID,DepartmentID,LevelID,Datetime,Del_tag", $"{FactoryID},{DepartmentID},{Level},'{DateTime.Now.Date}',{0}");
            //}
        }
    }
}